from flask import Flask, request, jsonify
from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # 这将为所有路由启用CORS

# 用于存储历史消息的列表

@app.route('/chat', methods=['GET',"POST"])
def get_message():
    """
    接收一个通过查询参数传递的消息，将其添加到历史记录中，并返回所有历史消息。
    """
# 判断有没有message参数
    message =""
    if 'mmm' in request.args:
        message = request.args.get('mmm')
    # 将message写入一个txt
    with open("message.txt", "a") as f:
        f.write(message)
        f.write("\n")
    # 将txt全部内容读出
    with open("message.txt", "r") as f:
        content = f.read()
    print(content)
    if content:
        return jsonify({"content":content,"msg":"消息记录成功"}), 200
    else:
        return jsonify({'error': 'No message provided as a query parameter.'}), 400

if __name__ == '__main__':
    app.run("0.0.0.0",debug=False)